public class L704 {

//    public static int search(int[] nums, int target) {
//        for (int i = nums.length - 1; i >= 0; i--) {
//            if (nums[i] == target) {
//                return i;
//            }
//        }
//        return -1;
//    }


    //    使用二分查找
    public static int search(int[] nums, int target) {
        int i = 0, j = nums.length - 1;

        while (i <= j) {
            int m = (i + j) / 2;
            if (nums[m] > target) {
                j = m - 1;
            } else if (nums[m] < target) {
                i = m + 1;
            } else {
                return m;
            }
        }
        return -1;
    }


    public static void main(String[] args) {

//        int[] nums = {-1, 0, 3, 5, 9, 12};
        int[] nums = {5};
        int target = 5;

        int index = search(nums, target);
        System.out.println(index);

    }
}
